package com.burst.reptile.modular.zp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.constant.HttpStatus;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.app.vo.FollowVo;
import com.burst.reptile.modular.zp.entity.ZpFans;
import com.burst.reptile.modular.zp.entity.ZpFollow;
import com.burst.reptile.modular.zp.mapper.ZpFansMapper;
import com.burst.reptile.modular.zp.mapper.ZpFollowMapper;
import com.burst.reptile.modular.zp.service.IZpFollowService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 用户关注列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-19
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpFollowServiceImpl extends ServiceImpl<ZpFollowMapper, ZpFollow> implements IZpFollowService {

	private final ZpFansMapper fansMapper;
	 
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int follow(Long userId) {
		int num = 0;
		// 新增我的关注列表
		ZpFollow follow = new ZpFollow();
		Long loginUserId = SecurityUtils.getLoginUserId();
		if(userId.equals(loginUserId)) {
			throw new CustomException("禁止操作", HttpStatus.UNAUTHORIZED);
		}
		follow.setUserId(loginUserId);
		follow.setUserToFollowId(userId);
		int count = this.count(new QueryWrapper<ZpFollow>().eq("user_id", loginUserId).eq("user_to_follow_id", userId));
		if(count == 0) {
			this.baseMapper.insert(follow);
		}
		// 新增关注用户的粉丝列表
		ZpFans fans = new ZpFans();
		fans.setUserId(userId);
		fans.setUserCoverFansId(loginUserId);
		Integer selectCount = this.fansMapper.selectCount(new QueryWrapper<ZpFans>().eq("user_id", userId).eq("user_cover_fans_id", loginUserId));
		if(selectCount == 0) {
			num = fansMapper.insert(fans);
		}
		return num;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int takeOff(Long userId) {
		// 删除我的关注列表
		Long loginUserId = SecurityUtils.getLoginUserId();
		this.baseMapper.delete(new QueryWrapper<ZpFollow>().eq("user_id", loginUserId).eq("user_to_follow_id", userId));
		// 删除关注用户的粉丝列表
		return fansMapper.delete(new QueryWrapper<ZpFans>().eq("user_id", userId).eq("user_cover_fans_id", loginUserId));
	}

	@Override
	public Page<ZpFollow> pageList(Page<ZpFollow> page, ZpFollow follow) {
		Long userId = follow.getUserId();
		QueryWrapper<ZpFollow> queryWrapper = new QueryWrapper<>();
		if(ToolUtil.isNotEmpty(userId)) {
			queryWrapper.eq("user_id", userId);
		}
		return this.page(page, queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int wxfollow(Long userId, Long auserId) {
		int num = 0;
		// 新增我的关注列表
		ZpFollow follow = new ZpFollow();
		follow.setUserId(auserId);
		follow.setUserToFollowId(userId);
		int count = this.count(new QueryWrapper<ZpFollow>().eq("user_id", auserId).eq("user_to_follow_id", userId));
		if(count == 0) {
			this.baseMapper.insert(follow);
		}
		// 新增关注用户的粉丝列表
		ZpFans fans = new ZpFans();
		fans.setUserId(userId);
		fans.setUserCoverFansId(auserId);
		Integer selectCount = this.fansMapper.selectCount(new QueryWrapper<ZpFans>().eq("user_id", userId).eq("user_cover_fans_id", auserId));
		if(selectCount == 0) {
			num = fansMapper.insert(fans);
		}
		return num;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int wxtakeOff(Long userId, Long auserId) {
		// 删除我的关注列表
		this.baseMapper.delete(new QueryWrapper<ZpFollow>().eq("user_id", auserId).eq("user_to_follow_id", userId));
		// 删除关注用户的粉丝列表
		return fansMapper.delete(new QueryWrapper<ZpFans>().eq("user_id", userId).eq("user_cover_fans_id", auserId));
	}

	@Override
	public Page<FollowVo> followlist(Page<FollowVo> page, Long userId, String key) {
		return this.baseMapper.followlist(page, userId, key);
	}

}
